﻿using Huirui.Cavan.Application.MainBoundedContext.SystemModule;
using Huirui.Cavan.Domain.MainBoundedContext.SearchModule;
using Huirui.Cavan.Domain.MainBoundedContext.SystemModule;
using Huirui.Cavan.Domain.Seedwork;
using Huirui.Cavan.Presentation.Seedwork;
using Huirui.Cavan.Presentation.ViewModel.SystemModule;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

namespace Huirui.Cavan.Presentation.Controller.SystemModule
{
    public class CurrencyController : EntityController<Currency, int, CurrencySearch, ICurrencyViewModel, ICurrencyAppService>, ICurrencyController
    {
        public override string ControllerName
        {
            get { return ControllerConst.CURRENCY; }
        }

        public override bool UseDenied
        {
            get { return false; }
        }

        public CurrencyController()
        {
            this.Model = ModelFactory.CreateModel<ICurrencyViewModel>();
        }

        [ActionName(ActionConst.ADD)]
        [HttpGet]
        [ActionPermissionAttribute(Name = "currency_query")]
        public ActionResult AddCurrency()
        {
            return View("~/Views/System/v1/addCurrency.cshtml");
        }

        [ActionName(ActionConst.ADD)]
        [HttpPost]
        [ActionPermissionAttribute(Name = "currency_edit")]
        public JsonResult AddCurrency(CurrencyViewModel model)
        {
            BoolResult jsonResult = null;
            BoolGenericResult<Currency> result = this.AppService.AddOrUpdate(model.Item);
            jsonResult = new BoolResult(result.IsSuccess, result.Messages);

            return Json(jsonResult, "application/json");
        }

        [ActionName(ActionConst.LIST)]
        [ActionPermissionAttribute(Name = "currency_query")]
        public JsonResult GetCurrencys()
        {
            CurrencyJsonViewModel json = new CurrencyJsonViewModel();
            json.rows = new List<CurrencyJson>();

            IList<Currency> currencys = this.AppService.GetBySearch(SearchFactory.CreateCurrencySearch()).ToList();
            foreach (Currency c in currencys)
            {
                CurrencyJson cj = new CurrencyJson();
                cj.Id = c.Id;
                cj.Code = c.Code;
                cj.Name = c.Name;
                cj.Rate = c.Rate;
                cj.IsLocal = c.IsLocal;
                cj.StrIsLocal = c.IsLocal ? "是" : "否";
                json.rows.Add(cj);
            }
            json.total = currencys.Count;

            return Json(json, "application/json");
        }

        [ActionName(ActionConst.DELETE)]
        [HttpPost]
        [ActionPermissionAttribute(Name = "currency_edit")]
        public JsonResult DeleteCurrency(string selected)
        {
            BoolGenericResult<Currency> result = null;
            List<CurrencyJson> currencys = JsonConvert.DeserializeObject<List<CurrencyJson>>(selected);

            foreach (CurrencyJson json in currencys)
            {
                Currency c = this.AppService.GetById(json.Id);
                result = this.AppService.Delete(c);
            }

            BoolResult jsonResult = new BoolResult(result.IsSuccess, result.Messages);

            return Json(jsonResult, "application/json");
        }
    }
}
